package com.qumitech.visualnovel.service.tip.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.tip.TipAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.tip.TipQueryDTO;
import com.qumitech.visualnovel.service.tip.TipService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/7/30 16:17
 */
@Service
public class TipServiceImpl implements TipService {

    private static final String tipUrlList = "/console/tip/list";
    private static final String tipUrlUpdate = "/console/tip";

    @Override
    public Map<String, Object> getTipList(TipQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + tipUrlList;
        Map<String, String> paramMap = new HashMap<>();
        if (queryDTO == null) {
            queryDTO = new TipQueryDTO();
        }
        if (queryDTO.getOffset() != null)
            paramMap.put("offset", queryDTO.getOffset().toString());
        if (queryDTO.getLimit() != null)
            paramMap.put("limit", queryDTO.getLimit().toString());
        if (queryDTO.getStartTime() != null)
            paramMap.put("startTime", queryDTO.getStartTime());
        if (queryDTO.getEndTime() != null)
            paramMap.put("endTime", queryDTO.getEndTime());
        if (queryDTO.getBeReportUid() != null)
            paramMap.put("be_report_uid", queryDTO.getBeReportUid().toString());
        if (queryDTO.getReportUid() != null)
            paramMap.put("report_uid", queryDTO.getReportUid().toString());
        if (queryDTO.getReportUid() != null)
            paramMap.put("report_uid", queryDTO.getReportUid().toString());
        if (queryDTO.getType() != null)
            paramMap.put("type", queryDTO.getType().toString());
        if (queryDTO.getState() != null)
            paramMap.put("state", queryDTO.getState().toString());

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateTip(TipAdminDTO dto) {
        dto.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + tipUrlUpdate+"/"+dto.getTipId();
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
